package com.gi.product.service.impl;

import com.gi.product.common.ProductInfoOutput;
import com.gi.product.constant.MyEnums;
import com.gi.product.constant.ResultEnum;
import com.gi.product.dto.CartDTO;
import com.gi.product.entity.ProductInfo;
import com.gi.product.entity.User;
import com.gi.product.error.ProductException;
import com.gi.product.mapper.ProductInfoMapper;
import com.gi.product.mapper.UserMapper;
import com.gi.product.service.ProductInfoService;
import com.gi.product.utils.JsonUtil;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public List<ProductInfo> findAllProductInfo() {
//        User user = userMapper.Sel(1);
//        List<User> allUser = userMapper.findAllUser();
//        System.out.println(user.toString());
        List<ProductInfo> allProductInfo = productInfoMapper.findAllProductInfo();
        System.out.println(allProductInfo.toString());
        return allProductInfo;
    }

    @Override
    public List<ProductInfo> findUpProductInfo() {
        List<ProductInfo> productInfoList = productInfoMapper.findUpProductInfo(MyEnums.UP.getCode());
        return productInfoList;
    }

    @Override
    public List<ProductInfo> findList(List<String> productIdList) {
        List<ProductInfo> list = productInfoMapper.findList(productIdList);
        return list;
    }

//    @Override
    @Transactional
    public void decreaseStock_old(List<CartDTO> cartDTOList) {
        for (CartDTO cartDTO : cartDTOList) {
            ProductInfo productInfo = productInfoMapper.findProductById(cartDTO.getProductId());

            //判断商品是否存在
            if (null==productInfo){
                throw new ProductException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            //库存是否足够
            Integer result = productInfo.getProductStock() - cartDTO.getProductQuantity();
            if (result < 0) {
                throw new ProductException(ResultEnum.PRODUCT_STOCK_ERROR);
            }

            productInfo.setProductStock(result);
            productInfoMapper.updateProductInfo(productInfo);
        }
    }

    /**
     * 扣库存并发送mq消息
     * @param cartDTOList
     */
    @Override
    public void decreaseStock(List<CartDTO> cartDTOList) {
        List<ProductInfo> productInfoList = decreaseStockProcess(cartDTOList);

        //发送mq消息
        List<ProductInfoOutput> productInfoOutputList = productInfoList.stream().map(e -> {
            ProductInfoOutput output = new ProductInfoOutput();
            BeanUtils.copyProperties(e, output);
            return output;
        }).collect(Collectors.toList());
        amqpTemplate.convertAndSend("productInfo", JsonUtil.toJson(productInfoOutputList));
//        amqpTemplate.convertAndSend("productInfo", productInfoOutputList);

    }

    @Transactional
    public List<ProductInfo> decreaseStockProcess(List<CartDTO> cartDTOList) {
        List<ProductInfo> productInfoList = new ArrayList<>();
        for (CartDTO cartDTO: cartDTOList) {
            ProductInfo productInfo = productInfoMapper.findProductById(cartDTO.getProductId());
            //判断商品是否存在
            if (null==productInfo){
                throw new ProductException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            //库存是否足够
            Integer result = productInfo.getProductStock() - cartDTO.getProductQuantity();
            if (result < 0) {
                throw new ProductException(ResultEnum.PRODUCT_STOCK_ERROR);
            }

            productInfo.setProductStock(result);
            productInfoMapper.updateProductInfo(productInfo);
            productInfoList.add(productInfo);
        }
        return productInfoList;
    }
}
